11. Car Object
Car Object
Creating a Car Object
To create a Car, which was named carla
in the example. I have to:
- Import our car file and define a car's initial state variable, and
- Call
car.Car()
; a special function that initializes a Car object, and pass in the initial state variables.
The state is defined by a position: [y, x] and a velocity, which has vertical and horizontal components: [vy, vx]. And lastly, we had to pass in a world, which is just a 2D array.
Imports and Defining initial variables
# Import statements
import numpy
import car
# Declare initial variables
# Create a 2D world of 0's
height = 4
width = 6
world = np.zeros((height, width))
# Define the initial car state
initial_position = [0, 0] # [y, x] (top-left corner)
velocity = [0, 1] # [vy, vx] (moving to the right)
Creating and Visualizing a Car!
# Create a car object with these initial params
carla = car.Car(initial_position, velocity, world)
# Display the world
carla.display_world()
Car movement
Carla can also move in the direction of the velocity and turnleft with the functions: move()
and turn_left()
.